﻿using DSPL.Contracts;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DSPL.Common
{
    /// <summary>
    /// Common Textbox tool for handeling Customer Details
    /// </summary>
    public partial class TxtCustomer : UserControl
    {
        public TxtCustomer()
        {
            InitializeComponent();
        }

        #region Declaring Properties

        #region General Properties

        public object CustomerKeyValue
        {
            get { return (object)GetValue(CustomerKeyValueProperty); }
            set { SetValue(CustomerKeyValueProperty, value); }
        }

        public string KeyFieldName
        {
            get { return (string)GetValue(KeyFieldNameProperty); }
            set { SetValue(KeyFieldNameProperty, value); }
        }

        public string KeyFieldType
        {
            get { return (string)GetValue(KeyFieldTypeProperty); }
            set { SetValue(KeyFieldTypeProperty, value); }
        }

        public DataRow SelectedDataRow
        {
            get { return (DataRow)GetValue(SelectedDataRowProperty); }
            set { SetValue(SelectedDataRowProperty, value); }
        }

        public Modes ParentFormMode
        {
            get { return (Modes)GetValue(ParentFormModeProperty); }
            set { SetValue(ParentFormModeProperty, value); }
        }

        public bool IsMandatory
        {
            get { return (bool)GetValue(IsMandatoryProperty); }
            set { SetValue(IsMandatoryProperty, value); }
        }

        #endregion

        #region txtCode Properties

        public Visibility CodeVisibility
        {
            get { return (Visibility)GetValue(CodeVisibilityProperty); }
            set { SetValue(CodeVisibilityProperty, value); }
        }

        public bool CodeEnable
        {
            get { return (bool)GetValue(CodeEnableProperty); }
            set { SetValue(CodeEnableProperty, value); }
        }

        public string CustomerCode
        {
            get { return (string)GetValue(CustomerCodeProperty); }
            set { SetValue(CustomerCodeProperty, value); }
        }

        public int CodeColumnSpan
        {
            get { return (int)GetValue(CodeColumnSpanProperty); }
            set { SetValue(CodeColumnSpanProperty, value); }
        }

        #endregion

        #region txtDescription Properties

        public Visibility DescriptionVisibility
        {
            get { return (Visibility)GetValue(DescriptionVisibilityProperty); }
            set { SetValue(DescriptionVisibilityProperty, value); }
        }

        public bool DescriptionEnable
        {
            get { return (bool)GetValue(DescriptionEnableProperty); }
            set { SetValue(DescriptionEnableProperty, value); }
        }

        public string CustomerDescription
        {
            get { return (string)GetValue(CustomerDescriptionProperty); }
            set { SetValue(CustomerDescriptionProperty, value); }
        }

        public int DescriptionColumnSpan
        {
            get { return (int)GetValue(DescriptionColumnSpanProperty); }
            set { SetValue(DescriptionColumnSpanProperty, value); }
        }

        public int DescriptionColumn
        {
            get { return (int)GetValue(DescriptionColumnProperty); }
            set { SetValue(DescriptionColumnProperty, value); }
        }

        public bool IsFocus
        {
            get { return (bool)GetValue(IsFocusProperty); }
            set { SetValue(IsFocusProperty, value); }
        }

        #endregion

        #region Other Customer Details Properties

        public string Address1
        {
            get { return (string)GetValue(Address1Property); }
            set { SetValue(Address1Property, value); }
        }

        public string Address2
        {
            get { return (string)GetValue(Address2Property); }
            set { SetValue(Address2Property, value); }
        }

        public string Address3
        {
            get { return (string)GetValue(Address3Property); }
            set { SetValue(Address3Property, value); }
        }

        public decimal CreditPeriod
        {
            get { return (decimal)GetValue(CreditPeriodProperty); }
            set { SetValue(CreditPeriodProperty, value); }
        }

        public decimal CreditLimit
        {
            get { return (decimal)GetValue(CreditLimitProperty); }
            set { SetValue(CreditLimitProperty, value); }
        }

        public string FilterString
        {
            get { return (string)GetValue(FilterStringProperty); }
            set { SetValue(FilterStringProperty, value); }
        }

        #endregion

        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty CustomerKeyValueProperty = DependencyProperty.Register("CustomerKeyValue", typeof(object), typeof(TxtCustomer), new PropertyMetadata(null));

        public static readonly DependencyProperty KeyFieldNameProperty = DependencyProperty.Register("KeyFieldName", typeof(string), typeof(TxtCustomer), new PropertyMetadata(""));

        public static readonly DependencyProperty KeyFieldTypeProperty = DependencyProperty.Register("KeyFieldType", typeof(string), typeof(TxtCustomer), new PropertyMetadata(null));

        public static readonly DependencyProperty SelectedDataRowProperty = DependencyProperty.Register("SelectedDataRow", typeof(DataRow), typeof(TxtCustomer), new PropertyMetadata(null));

        public static readonly DependencyProperty CodeVisibilityProperty = DependencyProperty.Register("CodeVisibility", typeof(Visibility), typeof(TxtCustomer), new PropertyMetadata(Visibility.Visible));

        public static readonly DependencyProperty CodeEnableProperty = DependencyProperty.Register("CodeEnable", typeof(bool), typeof(TxtCustomer), new PropertyMetadata(true));

        public static readonly DependencyProperty CustomerCodeProperty = DependencyProperty.Register("CustomerCode", typeof(string), typeof(TxtCustomer), new PropertyMetadata(""));

        public static readonly DependencyProperty DescriptionVisibilityProperty = DependencyProperty.Register("DescriptionVisibility", typeof(Visibility), typeof(TxtCustomer), new PropertyMetadata(Visibility.Visible));

        public static readonly DependencyProperty DescriptionEnableProperty = DependencyProperty.Register("DescriptionEnable", typeof(bool), typeof(TxtCustomer), new PropertyMetadata(false));

        public static readonly DependencyProperty CustomerDescriptionProperty = DependencyProperty.Register("CustomerDescription", typeof(string), typeof(TxtCustomer), new PropertyMetadata(""));

        public static readonly DependencyProperty ParentFormModeProperty = DependencyProperty.Register("ParentFormMode", typeof(Modes), typeof(TxtCustomer), new PropertyMetadata(Modes.Undo));

        public static readonly DependencyProperty IsMandatoryProperty = DependencyProperty.Register("IsMandatory", typeof(bool), typeof(TxtCustomer), new PropertyMetadata(false));

        public static readonly DependencyProperty CodeColumnSpanProperty = DependencyProperty.Register("CodeColumnSpan", typeof(int), typeof(TxtCustomer), new PropertyMetadata(1));

        public static readonly DependencyProperty DescriptionColumnSpanProperty = DependencyProperty.Register("DescriptionColumnSpan", typeof(int), typeof(TxtCustomer), new PropertyMetadata(1));

        public static readonly DependencyProperty DescriptionColumnProperty = DependencyProperty.Register("DescriptionColumn", typeof(int), typeof(TxtCustomer), new PropertyMetadata(1));

        public static readonly DependencyProperty IsFocusProperty = DependencyProperty.Register("IsFocus", typeof(bool), typeof(TxtCustomer), new PropertyMetadata(false));

        public static readonly DependencyProperty Address1Property = DependencyProperty.Register("Address1", typeof(string), typeof(TxtCustomer), new PropertyMetadata(""));

        public static readonly DependencyProperty Address2Property = DependencyProperty.Register("Address2", typeof(string), typeof(TxtCustomer), new PropertyMetadata(""));

        public static readonly DependencyProperty Address3Property = DependencyProperty.Register("Address3", typeof(string), typeof(TxtCustomer), new PropertyMetadata(""));

        public static readonly DependencyProperty CreditLimitProperty = DependencyProperty.Register("CreditLimit", typeof(decimal), typeof(TxtCustomer), new PropertyMetadata(0.0M));

        public static readonly DependencyProperty CreditPeriodProperty = DependencyProperty.Register("CreditPeriod", typeof(decimal), typeof(TxtCustomer), new PropertyMetadata(0.0M));

        public static readonly DependencyProperty FilterStringProperty = DependencyProperty.Register("FilterString", typeof(string), typeof(TxtCustomer), new PropertyMetadata(""));

        #endregion

        #region Exposable Event Handlers

        public event EventHandler<EventArgs> RefreshCustomerRelatedData;             //use to refresh data regarding Customer Master Text box

        #endregion

        #region Internal Events

        private void RefreshAttachedObjects(object sender, EventArgs e)
        {
            if (SelectedDataRow != null)
            {
                //if (!string.IsNullOrEmpty(FilterString))
                //{
                //    DataTable dt = new DataTable();
                //    dt.Rows.Add(SelectedDataRow);
                //    if (dt.Select(FilterString).Count() != 1)
                //        ClearData();
                //    return;
                //}
                Address1 = SelectedDataRow["DebAdd1"].ToString();
                Address2 = SelectedDataRow["DebAdd2"].ToString();
                Address3 = SelectedDataRow["DebAdd3"].ToString();
                CreditLimit = (decimal)SelectedDataRow["CrdLimit"];
                CreditPeriod = (decimal)SelectedDataRow["CrdPeriod"];
            }
            else
                ClearData();

            if (RefreshCustomerRelatedData != null)
                RefreshCustomerRelatedData(sender, e);
        }

        private void ClearData()
        {
            CustomerKeyValue = null;
            CustomerCode = "";
            CustomerDescription = "";
            Address1 = "";
            Address2 = "";
            Address3 = "";
            CreditLimit = 0;
            CreditPeriod = 0;
        }

        #endregion
    }
}
